package com.example.petcare.service.serviceImpl;

import com.example.petcare.exception.ReviewException;
import com.example.petcare.mapper.ReviewMapper;
import com.example.petcare.pojo.Pet;
import com.example.petcare.pojo.Review;
import com.example.petcare.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ReviewServiceImpl implements ReviewService {

    // 注入ReviewMapper
    @Autowired
    private ReviewMapper reviewMapper;

    // 添加评论
    @Override
    public void addReview(Review review) {

        // 先查询是否评价过，如果评价过，则不能再评价,下面是代码
        Review review1 = reviewMapper.getReviewByHostId(review.getHostId());
        if (review1 != null) {
            throw new ReviewException("已经评价过了，不能再评价");
        }

        // 设置时间
        review.setReviewTime(LocalDateTime.now());
        // 调用mapper方法
        reviewMapper.addReview(review);
    }

    // 根据providerId查询评论
    @Override
    public List<Review> list(Integer providerId) {
        // 调用mapper方法
        List<Review> reviews = reviewMapper.getReviewByProviderId(providerId);
        if (reviews == null) {
            throw new ReviewException("没有评论");
        }
        return reviews;
    }



}
